<template>
  <div>
    <el-card class="box-card smallwidth">
      <div slot="header" class="clearfix">
        <el-form
          :label-position="labelPosition"
          label-width="80px"
          :model="formLabelAlign"
          style="width:87%"
        >
          <el-form-item label="用户名">
            <el-input v-model="formLabelAlign.name"></el-input>
          </el-form-item>
          <el-form-item label="密码">
            <el-input v-model="formLabelAlign.region"></el-input>
          </el-form-item>
        </el-form>
        <el-button type="success" round @click="login">登录</el-button>
        <el-button type="warning" round  v-if="registervisible" @click="dialogVisible = true">信息错误。试试注册？</el-button>
      </div>
    </el-card>
    <el-dialog title="注册" :visible.sync="dialogVisible" width="40%">
      <el-form :model="form">
        <el-form-item label="用户名" :label-width="formLabelWidth" style="width:300px">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="密码" :label-width="formLabelWidth" style="width:300px">
          <el-input v-model="form.password" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item :label-width="formLabelWidth" style="width:300px">
          <el-button type="success" round @click="reg">注册</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';

export default {
  //import引入的组件需要注入到对象中才能使用
  components: {},
  props: {},
  data() {
    //这里存放数据
    return {
      form: {
        name: "",
        password: ""
      },
      formLabelWidth: "120px",
      dialogVisible: false,
      registervisible: false,
      labelPosition: "right",
      status: " ",
      formLabelAlign: {
        name: "",
        region: ""
      }
    };
  },

  methods: {
    login() {
      console.log(this.formLabelAlign);
      this.$http
        .post("http://123.57.56.140:11111/service/user/login", {
          // .post("/api/user/logins", {
          username: this.formLabelAlign.name,
          password: this.formLabelAlign.region
        })
        .then(({ data }) => {
          console.log(data);
          if (data.code == 0) {
            this.status = "<P>正在跳转</P>";
            this.$router.push({
              name: "document",
              params: {
                username: this.formLabelAlign.name
              }
            });
          } else {
            this.registervisible = true;
          }
        })
        .catch(error => {
          console.error("网络错误");
        });
    },
    reg() {
      var a = this.form.name;
      var b = this.form.password;
      this.$http
        .post("http://123.57.56.140:11111/service/user/save", {
          username: a,
          password: b
        })
        .then(data => {
          console.log(data);
          this.$http
            .post("http://123.57.56.140:11111/hdfs/do/list/mkdir", {
              url: "hdfs://localhost:9000",
              newdir: this.form.name
            })
            .then(data => {
              console.log("创建用户的hdfs");
              this.dialogVisible=false;
            });
        });
    }
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {},
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {} //如果页面有keep-alive缓存功能，这个函数会触发
};
</script>
<style scoped>
.smallwidth {
  margin: 0 auto;
  width: 500px;
}
p {
  color: brown;
}
</style>